package com.lch.designpattern.singleton;

import com.lch.designpattern.singleton.seriable.SeriableSingleton;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author: liuchenhui
 * @create: 2019-04-16 23:48
 **/
public class SeriableSingletonTest {
    public static void main(String[] args) {
        SeriableSingleton singleton1 = null;
        SeriableSingleton singleton2 = SeriableSingleton.getInstance();

        try {
            FileOutputStream fos  = new FileOutputStream("seriable.obj");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(singleton2);
            oos.flush();
            oos.close();

            FileInputStream fis = new FileInputStream("seriable.obj");
            ObjectInputStream ois = new ObjectInputStream(fis);
            singleton1 = (SeriableSingleton) ois.readObject();
            ois.close();

            System.out.println(singleton1 == singleton2);
        } catch (Exception e) {

        }
    }
}
